package BlockR.function;

import BlockR.model.RandomNumOBJ;
import BlockR.util.Byte_XOR;

public class Decrypt_Other_Block {
    public static byte[][][] Do_Decrypt_Other_Piece(byte[][][] All_Block, RandomNumOBJ[] All_Random_Num, int G, int arraySize) {
        long start_encrypt_other = System.currentTimeMillis();
        for (int i = 1; i <= MappedBiggerFileReader.getBlock_index(); i++) {    //从第一块开始遍历
            byte[][] index_block = All_Block[i];    //第i块
            if (index_block == null) {
                break;
            } else {
                int temp = arraySize * 8 / 2;
                int x = (17 * i + G) % temp;   //(17*(file_num+G))mod512    i是片数
                int y = (13 * (i + G) + i) % temp + temp; //(13*(file_num+G)+file_num)mod512+512
                All_Random_Num[i] = new RandomNumOBJ(RandomNum.generate_block_random_num_1(All_Block[i - 1][x],
                        All_Random_Num[i - 1].getR1(), x,arraySize), RandomNum.generate_block_random_num_2(All_Block[i - 1][y],
                        All_Random_Num[i - 1].getR2(), y,arraySize));
/*                int x = (17 * file_num + G) % 512;   //(17*(file_num+G))mod512
                int y = (13 * (file_num + G) + file_num) % 512 + 512; //(13*(file_num+G)+file_num)mod512+512
                All_Random_Num[file_num] = new RandomNumOBJ(RandomNum.generate_block_random_num_1(All_Block[file_num - 1][x],
                        All_Random_Num[file_num - 1].getR1(), x), RandomNum.generate_block_random_num_2(All_Block[file_num - 1][y],
                        All_Random_Num[file_num - 1].getR2(), y));*/
//                All_Random_Num[file_num] = new RandomNumOBJ(RandomNum.generate_test_random_num(128), RandomNum.generate_test_random_num(128));
//                byte[] key = Byte_XOR.XOR(All_Random_Num[file_num].getR1(), All_Random_Num[file_num].getR2());
                int end = 0;
                if (i == MappedBiggerFileReader.getBlock_index()) {    //如果此块是最后一块则不加密最后一片
                    end = MappedBiggerFileReader.getPiece_index() % (arraySize * 8) - 1;
                } else {
                    end = index_block.length;
                }
                for (int j = 0; j < end; j++) {
                    if (i == 2 && j == 1000) {
                        int k = 0;
                    }
                    byte[] index_piece = index_block[j];    //第j片
                    if (index_piece == null) {
                        break;
                    } else {
                        byte[] index_random_num_1 = RandomNum.generate_index_random_num_1(All_Random_Num[i].getR1(), j % (arraySize * 8),arraySize);
                        int shift_X = ((G % temp) * (j * 8 + G) + 19) % temp;   //(Gmod512*(8j+G)+19)mod512   j是片数
                        byte[] index_random_num_2 = RandomNum.generate_index_random_num_2(All_Random_Num[i].getR2(), shift_X,arraySize);
                        byte[] key = Byte_XOR.XOR(index_random_num_1, index_random_num_2);
                        index_piece = Byte_XOR.XOR(index_piece, key);
                        index_block[j] = index_piece;   //替换旧片
                    }
                }
                All_Block[i] = index_block; //替换旧块
            }
        }
        long end_encrypt_other = System.currentTimeMillis();
        System.out.println("\n解密剩余部分耗时：" + String.valueOf(end_encrypt_other - start_encrypt_other));
        return All_Block;
    }
}
